<?php
/**
 * Created by PhpStorm.
 * User: 王文凡<787802868@qq.com>
 * Date: 2016/9/19 0019
 * Time: 14:20
 */

namespace Admin\Controller;


use Common\Controller\AuthController;
use Think\Controller;

class OrderSetController extends AuthController
{

    //订单商品列表
    public function baseSeting()
    {
        $counts=M('OrderProduct')->where("status=%d",1)->count();
        $page=page($counts,10,5);
        $re=M('OrderProduct')->where("status=%d",1)->select();
        $list=array_slice(oneL($re),$page->firstRow,$page->listRows);
        $this->assign('_list',$list);
        $this->assign('_page',$page->show());
        $this->assign('title','订单商品列表');
        $this->display();
    }
    //新增商品名称
    public function addProduct($id='')
    {
        if(IS_POST){
            if($_POST['id']){
                $id=I('post.id');
            }
            $arr=D('OrderProduct')->addProduct($id,$_POST);
            if($arr['state']=='success'){
                $this->success($arr['info']);
            }else{
                $this->error($arr['info']);
            }
        }else{
            $re=M('OrderProduct')->where("id=%d",$id)->find();
            $list=D('OrderProduct')->productList();
            $this->assign('_list',$list);
            $this->assign('edit',$re);
            $this->assign('title','新增订单商品');
            $this->display();
        }


    }
    //删除商品
    public function mdel($id='')
    {
        if(IS_AJAX){
            if(M('OrderProduct')->where("pid=%d",$id)->find()){
                $this->error('请先删除下面的子分类','',IS_AJAX);
                exit;
            }
            if(M('OrderProduct')->where("id=%d",$id)->save(['status'=>-1]) ){
                $this->success('删除成功','',IS_AJAX);
            }else{
                $this->error('删除失败','',IS_AJAX);
            }
        }
    }
    //订单参数列表
    public function parameterList(){
        $model=M('OrderParameter');
        $counts=$model->alias("a")->join("sr_order_product as b ON a.cate_pid=b.id")->count();
        $page=page($counts,10,5);
        $list=$model->alias("a")->field("a.*,b.cate_name")->join("sr_order_product as b ON a.cate_pid=b.id")->select();
        $this->assign('_list',$list);
        $this->assign('_page',$page->show());
        $this->display();
    }


    //新增参数设置
    public function parameterSet($id='')
    {

        if(IS_POST){
            $model=D('OrderParameter');
             if(!$model->create()){
                 $this->error($model->getError());
                 exit;
             }
            if(!empty($id)){
                if($model->where("id=%d",$id)->save($_POST)){
                    $this->success('更新成功');
                }else{
                    $this->error('更新失败');
                }
            }else{
                if($model->add($_POST)){
                    $this->success('新增成功',U('OrderSet/parameterList'));
                }else{
                    $this->error('新增失败');
                }
            }

        }else{
            $list=M('OrderProduct')->field("id,cate_name")->where("pid=%d and status=%d",[0,1])->select();//一级分类信息
            if(!empty($id)){
                $re=M('OrderParameter')->where("id=%d",1)->find();//参数信息
                $this->assign('re',$re);
            }
            $this->assign('_list',$list);
            $this->display();
        }

    }

    //颜色管理
    public function colorset(){
        $arr=D('Color')->lists();
        $this->assign('_list',$arr['list']);
        $this->assign('_page',$arr['page']->show());
        $this->display();
    }
    //新增颜色
    public function addcolor($id=null){

        if(IS_POST){
            $model=D('Color');
            if(!$model->create()){
                $this->error($model->getError());
            }else{
                if($id == null){
                    if($model->add()){
                        $this->success('新增成功',U('OrderSet/colorset'));
                    }else{
                        $this->error('新增失败');
                    }
                }else{
                    if($model->where("id=%d",$id)->save($_POST)){
                        $this->success('更新成功');
                    }else{
                        $this->error('更新失败');
                    }
                }
            }
        }else{
            if(!empty($id)){
                $edit=M('Color')->where("id=%d",1)->find();
                $this->assign('edit',$edit);
            }
            $this->display();
        }

    }

    //删除颜色
    public function colordel($id=null){
        if(IS_AJAX){
            if(M('Color')->where("id=%d",$id)->delete()){
                $this->success('删除成功','',IS_AJAX);
            }else{
                $this->error('删除失败','',IS_AJAX);
            }
        }
    }

    //删除订单参数
    public function parameterdel($id=null){
        if(IS_AJAX){
            if(M('OrderParameter')->where("id=%d",$id)->delete()){
                $this->success('删除成功','',IS_AJAX);
            }else{
                $this->error('删除失败','',IS_AJAX);
            }
        }
    }



    //工艺设置
    public function craftSet()
    {
        $counts=M('Crafts')->where("status=%d",1)->count();
        $page=page($counts,10,5);
        $re=M('Crafts')->where("status=%d",1)->select();
        $list=array_slice(oneL($re),$page->firstRow,$page->listRows);
        $this->assign('_list',$list);
        $this->assign('_page',$page->show());
        $this->assign('title','工艺信息列表');

        $this->display();
    }
    //新增工艺信息
    public function addCraf($id='')
    {
        if(IS_POST){
            if($_POST['id']){
                $id=I('post.id');
            }

            $arr=D('Crafts')->addCraft($id,$_POST);
            if($arr['state']=='success'){
                $this->success($arr['info']);
            }else{
                $this->error($arr['info']);
            }
        }else{
            $re=M('Crafts')->where("id=%d",$id)->find();
            $list=D('Crafts')->craftList();
            $this->assign('_list',$list);
            $this->assign('edit',$re);
            $this->assign('title','新增订单商品');
            $this->display();
        }


    }
    //删除工艺
    public function cdel($id='')
    {
        if(IS_AJAX){
            if(M('Crafts')->where("pid=%d",$id)->find()){
                $this->error('请先删除下面的子分类','',IS_AJAX);
                exit;
            }
            if(M('Crafts')->where("id=%d",$id)->save(['status'=>-1]) ){
                $this->success('删除成功','',IS_AJAX);
            }else{
                $this->error('删除失败','',IS_AJAX);
            }
        }
    }

    //根据父级id获取商品名称
    public function productList()
    {
        if(IS_AJAX){
            $pid=I('post.pid');
            $list= M('OrderProduct')->field("id,cate_name")->where("status=%d and pid=%d",[1,$pid])->select();
            $this->ajaxReturn($list);
        }
    }

    //根据父结点查询工艺信息
    public function craftsList()
    {
        if(IS_AJAX){
            $pid=I('post.pid');
            $list=M('Crafts')->field("id,craft_name")->where("status=%d and pid=%d",[1,$pid])->select();
            $this->ajaxReturn($list);
        }
    }
    //线条型号设置
    public function linetype()
    {
        $counts=M('Linetypes')->where("status=%d",1)->count();
        $page=page($counts,10,5);
        $re=M('Linetypes')->where("status=%d",1)->select();
        $list=array_slice(oneL($re),$page->firstRow,$page->listRows);
        $this->assign('_list',$list);
        $this->assign('_page',$page->show());
        $this->assign('title','线条型号列表');
        $this->display();
    }
    //新增线条型号
    public function addline($id='')
    {
        if(IS_POST){
            if($_POST['id']){
                $id=I('post.id');
            }

            $arr=D('Linetypes')->addCraft($id,$_POST);
            if($arr['state']=='success'){
                $this->success($arr['info']);
            }else{
                $this->error($arr['info']);
            }
        }else{
            $re=M('Linetypes')->where("id=%d",$id)->find();
            $list=D('Linetypes')->lineList();
            $this->assign('_list',$list);
            $this->assign('edit',$re);
            $this->assign('title','新增订单商品');
            $this->display();
        }

    }
    //删除线条型号
    public function linedel($id='')
    {
        if(IS_AJAX){
            if(M('Linetypes')->where("pid=%d",$id)->find()){
                $this->error('请先删除下面的子分类','',IS_AJAX);
                exit;
            }
            if(M('Linetypes')->where("id=%d",$id)->save(['status'=>-1]) ){
                $this->success('删除成功','',IS_AJAX);
            }else{
                $this->error('删除失败','',IS_AJAX);
            }
        }

    }

    //线条长度设置
    public function linelenth()
    {

        if(IS_POST){

            $arrval=putarry($_POST,'lenth_value');

            foreach($arrval as $v){
                if($v['id']){
                    $arr=M('LineLenth')->where("id=%d",$v['id'])->save($v);
                }else{
                    $arr=M('LineLenth')->add(['lenth_value'=>$v['lenth_value']]);
                }

            }
                $this->success('操作成功');

        }else{
            $lengthlist=M('LineLenth')->where("status=%d",1)->select();
            $this->assign('lengthlist',$lengthlist);
            $this->assign('title','线条长度设置');
            $this->display();
        }

    }

    //获取线条型号
    public function linelist()
    {
        if(IS_AJAX){
            $pid=I('post.pid');
            $list=M('Linetypes')->field("id,line_name")->where("status=%d and pid=%d",[1,$pid])->select();
            $this->ajaxReturn($list);
        }
    }

    //删除订单长度信息
    public function lengthdel($id='')
    {
        if(M('LineLenth')->where("id=%d",$id)->save(['status'=>-1])){
            $this->success('删除成功','',IS_AJAX);
        }else{
            $this->error('删除失败','',IS_AJAX);
        }
    }

    //财务订单审核
    public function financePass()
    {
        if(IS_AJAX){
            $types=I('post.types');
            $info=M('Orders')->where("rid=%d",I('post.rid'))->find();
            if($types==1){
                $infotype=$info['is_finance'];//财务
                $is_send='is_finance';
                $is_person='finance';
            }else{
                $infotype=$info['is_audit'];//审单
                $is_send='is_audit';
                $is_person='auditPeople';
            }
            if($infotype==1){
                echo 102;
                die;
            }
            $_POST['is_pass']=1;
            $_POST['admin_id']=$_SESSION['tc']['uid'];
            $_POST['admin_nickname']=$_SESSION['tc']['nickname'];
            $_POST['create_time']=time();
            $obj=M('AuditLog');
            $obj->startTrans();//开启事务
            try{
              $obj->add($_POST);
              M('Orders')->where("rid=%d",I('post.rid'))->save([$is_send=>1,$is_person=>$_SESSION['tc']['nickname']]);
              $obj->commit();//成功则提交事务
                echo 200;
            }catch (Exception $e){
              $obj->rollback();//不成功，则回滚事务
                echo 101;

            }
        }


    }

    //门型设置
    public function goalpost(){
        $counts=M('Goals')->where("status=%d",1)->count();
        $page=page($counts,10,5);
        $list=M('Goals')->where("status=%d",1)->limit($page->firstRow,$page->listRows)->select();
        $this->assign('_list',$list);
        $this->assign('_page',$page->show());
        $this->assign('title','门型设置');
        $this->display();
    }
    //新增门型
    public function addgoal($id=null){
        if(IS_POST){
           $model=D('Goals');
            if(!$model->create()){
                $this->error($model->getError());
            }else{
              if(empty($id)){
                  if($model->add()){
                      $this->success('新增成功');
                  }else{
                      $this->error('新增失败');
                  }
              }else{
                  if(M('Goals')->where("id=%d",$id)->save($_POST)){
                      $this->success('修改成功');
                  }else{
                      $this->error('修改失败');
                  }
              }
            }
        }else{
         if(!empty($id)){
             $re=M('Goals')->where("id=%d",$id)->find();
             $this->assign('re',$re);
         }
         $this->display();
        }

    }
    //删除门型
    public function goaldel($id=null){
        if(IS_AJAX){
            if(M('Goals')->where("id=%d",$id)->delete()){
                $this->success('删除成功','',IS_AJAX);
            }else{
                $this->error('删除失败','',IS_AJAX);
            }
        }
    }

}













